﻿using System.Windows;
using System.Windows.Controls;

namespace TeamTasker.Client.Presentation.Controls.SearchInTreeControl
{
    /// <summary>
    /// Interaction logic for SearchInTreeControl.xaml
    /// </summary>
    public partial class SearchInTreeControl : UserControl
    {
        public static readonly DependencyProperty SearchInSourceProperty =
            DependencyProperty.Register("SearchInSource", typeof (INodeModelHierarchicalCollection),
                                        typeof (SearchInTreeControl));

        public static readonly DependencyProperty TextToSearchProperty =
            DependencyProperty.Register("TextToSearch", typeof (string), typeof (SearchInTreeControl),
                                        new PropertyMetadata(OnTextChanged));

        public SearchInTreeControl()
        {
            InitializeComponent();
        }

        public INodeModelHierarchicalCollection SearchInSource
        {
            get { return (INodeModelHierarchicalCollection) GetValue(SearchInSourceProperty); }
            set { SetValue(SearchInSourceProperty, value); }
        }

        public string TextToSearch
        {
            get { return (string) GetValue(TextToSearchProperty); }
            set { SetValue(TextToSearchProperty, value); }
        }

        private static void OnTextChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var control = d as SearchInTreeControl;
            if (control != null)
            {
                control.SearchInSource.FilterByNodeLabel(control.TextToSearch);
            }
        }
    }
}